package org.enlongWu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.enlongWu.mapper.MessageMapper;
import org.enlongWu.pojo.Message;
import org.enlongWu.pojo.PageBean;
import org.enlongWu.pojo.Result;
import org.enlongWu.service.MessageService;
import org.enlongWu.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;


    @Override
    public void add(Message message) {
//
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userId = (Integer) map.get("id");
//        message.setReceiveUser(userId);//设置收件者id
        messageMapper.add(message);
    }

    /**
     * 当前用户信箱
     * 首先创建一个PageBean对象，然后使用PageHelper插件进行分页查询。
     * 接着调用mapper接口的list方法获取列表，并将结果封装到PageBean对象中返回。
     *
     * @param pageNum     当前页码
     * @param pageSize    每页显示的记录数
     * @param messageType 分类ID
     * @param state       文章状态
     * @return
     */
    @Override
    public PageBean<Message> list(Integer pageNum, Integer pageSize, Integer messageType, String state) {
        //1.创建PageBean对象
        PageBean<Message> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Message> as = messageMapper.list(userId, messageType, state);//查询到的文章
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<Message> p = (Page<Message>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Result reading(Integer messageid) {
        Message message = messageMapper.searchById(messageid);
        if (message == null) {
            return Result.error("查无此号");//查无此号
        } else if (message.getState().equals("已读")) {
            return Result.error("本就已读");//本就已读
        } else {
            messageMapper.setRead(messageid);
            return Result.success("文章[" + message.getTitle() + "]标记成功");//操作成功
        }
    }

    @Override
    public List<Message> applicationGroupBySendTime(Integer senderId) {
        return messageMapper.applicationGroupBySendTime(senderId);
    }

    @Override
    public Message findById(Integer messageId) {
        return messageMapper.searchById(messageId);
    }

    @Override
    public Result delete(Integer messageId) {
        int i = messageMapper.delete(messageId);
        if (i == 1) {
            return Result.success(null,"删除了"+i+"条消息");
        }else {
            return Result.error("删除失败");
        }
    }

    @Override
    public int deleteByContent(Integer applicationId) {
        return messageMapper.deleteByContent(applicationId.toString());
    }


}
